from link import loginSearch
import tkinter as tk
from PIL import ImageTk, Image
from tkinter import messagebox

class visitorSignup():
    def __init__(self):
        #子窗口使用tk.Toplevel()声明
        self.window = tk.Toplevel()
        self.window.title('新用户注册')
        self.window.geometry('650x750')
        self.name = tk.StringVar(master=self.window)
        self.pwd = tk.StringVar(master=self.window)
        self.create_page()

    def create_page(self):
        image = Image.open('emt2.png')
        img = ImageTk.PhotoImage(image)
        canvas = tk.Canvas(self.window, height=750, width=640)
        canvas.create_image(0, 0, image=img, anchor='nw')

        tk.Label(self.window, text="账号", font=('楷体', 25), bg='CornflowerBlue', fg='Moccasin').place(x=10, y=150, anchor='nw')
        tk.Entry(self.window, textvariable=self.name, font=('microsoft yahei', 20), bd=4).place(x=100, y=150, anchor='nw')

        tk.Label(self.window, text="密码", font=('楷体', 25), bg='CornflowerBlue', fg='Moccasin').place(x=10, y=250, anchor='nw')
        tk.Entry(self.window, textvariable=self.pwd, font=('microsoft yahei', 20), show='*', bd=4).place(x=100, y=250, anchor='nw')

        tk.Button(self.window, text='注册', font=('楷体', 25), bg='CornflowerBlue', fg='Moccasin', command=self.signup).place(x=200, y=380, anchor='nw')
        canvas.pack()
        self.window.mainloop()


    def signup(self):
        #注册的时候账号不可以重复，但是密码可以重复
        obj = loginSearch()
        result = obj.get_info_v()
        username = self.name.get()
        pwd = self.pwd.get()

        flag = True

        if result:
            #当前注册表非空
            for name in result.keys():
                if str(name) == username:
                    messagebox.showinfo(title='', message='用户已存在')
                    flag = False

            if flag:
                #如果当前用户名未被注册
                obj.insert_info_v(int(username), pwd)

        else:
            #当前注册表为空
            obj.insert_info_v(int(username), pwd)

        obj.close_conn()
        
        self.window.destroy()